package com.android.xunxian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.MyApplication;
import com.android.xunxian.R;
import com.android.xunxian.adapter.GuanWTypeAdater;
import com.android.xunxian.entry.GuanwType;
import com.android.xunxian.utils.Util;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2016/8/14.
 */
public class GuanWActivity extends Activity implements View.OnClickListener {

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.listview)
    ListView listview;
    GuanWTypeAdater adater;
    private List<GuanwType> guanwTypeList;
    private List<Boolean> falg;
    private String selcet;//是否选中，本地保存
    private Util util;
    private String[] array;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guangw_activity);
        ButterKnife.inject(this);
        titleText.setText("管网设置");
        close.setOnClickListener(this);
        util = new Util(this);
        selcet = util.getUser("guanw");
        ritghText.setText("保存");
        System.out.println("selcet---" + selcet);
        ritghText.setOnClickListener(this);
        if (Util.NoNull(selcet)) {
            if (selcet.contains(",")) {
                array = selcet.split(",");
            } else {
                array = new String[1];
                array[0] = selcet;
            }

        }
        try {
            guanwTypeList = MyApplication.dbManager.selector(GuanwType.class).findAll();
            if (null != guanwTypeList && guanwTypeList.size() > 0) {
                ritghText.setVisibility(View.VISIBLE);
                falg = new ArrayList<>();
                for (int i = 0; i < guanwTypeList.size(); i++) {
                    if (null != array && array.length > 0) {

                        for (int j = 0; j < array.length; j++) {
                            if (guanwTypeList.get(i).getSELCON().equals(array[j])) {
                                falg.add(true);
                                break;
                            } else {
                                if (j == array.length - 1) {
                                    falg.add(false);
                                }
                            }

                        }
                    } else {
                        falg.add(false);
                    }
                }
                System.out.print(falg);
                adater = new GuanWTypeAdater(this, guanwTypeList, falg);
                listview.setAdapter(adater);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
                checkBox.toggle();
                if (checkBox.isChecked()) {
                    falg.set(position, true);
                } else {
                    falg.set(position, false);
                }
                System.out.println(falg);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:

                if (guanwTypeList != null && guanwTypeList.size() > 0) {
                    selcet = "";
                    for (int i = 0; i < falg.size(); i++) {
                        System.out.println("guanwTypeList.get(i).getSORTINDEX()==" + guanwTypeList.get(i).getSORTINDEX());
                        if (falg.get(i)) {
                            if (selcet.length() <= 0) {
                                System.out.println("AAAAAAAAAAA");
                                selcet = guanwTypeList.get(i).getSELCON();
                            } else {
                                System.out.println("BBBBBBBBBBBBBBBBB");
                                selcet = selcet + "," + guanwTypeList.get(i).getSELCON();
                            }
                        } else {
                            System.out.println("CCCCCCCCCCCCC");
                        }
                    }
                    System.out.println("selcet------" + selcet);
                    util.setTime("guanw", selcet);
                    Toast.makeText(getApplicationContext(), "保存成功", 100).show();
                }
                break;
        }
    }
}
